﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Windows.Forms;
using NIKE_Project.DAL;

namespace NIKE_Project.UI
{
    public partial class Visualization : Form
    {
        //调用DBHelper类
        DBHelper db = new DBHelper();

        //定义DataSet全局变量
        public DataSet ds;
        public Visualization()
        {
            InitializeComponent();
            Char_2_pintus();
            char_1_zzt();
        }

        private void chart1_Click(object sender, EventArgs e)
        {

        }

        private void chart2_Click(object sender, EventArgs e)
        {
            //string GoodsID = "select GoodsID from SalesDetail";
            //ds = db.getDataSet(GoodsID);




            // string GoodsID = "select GoodsID from SalesDetail";
            // ds = db.getDataSet(GoodsID);

            // List<T> xData = (List<T>)DataSetToList<T>(ds, 0);

            // //List<string> xData = new List<string>() {};
            // string Quantity = "select Quantity from SalesDetail";
            //DataSet ds2 = db.getDataSet(Quantity);
            // List<T> yData = (List<T>)DataSetToList<T>(ds2, 0);
            //List<int> yData = new List<int>() { 10, 20, 30, 40 };
            chart1.Series[0]["PieLabelStyle"] = "Outside";//将文字移到外侧 
            chart1.Series[0]["PieLineColor"] = "Black";//绘制黑色的连线。 
           // chart1.Series[0].Points.DataBindXY(xData, yData);
        }


        public void Char_2_pintus()
        {
            List<string> xData = new List<string>();
            List<int> yData = new List<int>();
            string GoodsID = "select goodsName,sum(Quantity) from SalesDetail as SalesDetail join Goods on Goods.GoodsID = SalesDetail.GoodsID group by GoodsName";
            ds = db.getDataSet(GoodsID);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                var a = row;
                xData.Add(row[0].ToString());
                yData.Add(Convert.ToInt32(row[1].ToString()));
            }


            chart2.Series[0]["PieLabelStyle"] = "Outside";//将文字移到外侧 
            chart2.Series[0]["PieLineColor"] = "Black";//绘制黑色的连线。 
            chart2.Series[0].Points.DataBindXY(xData, yData);
            //chart2.Series[0].Label = "#PERCENT{P2}";
        }

        public void char_1_zzt()
        {
            string sqls = "select  SalesDate,sum(Amount) as Amount from Sales group by SalesDate order by SalesDate desc";
            List<string> xData = new List<string>();
            List<double> yData = new List<double>();
            ds = db.getDataSet(sqls);
            string datas = "";
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                datas = row[0].ToString();
                datas = datas.Substring(0,datas.Length - 9);
                xData.Add(datas);
                yData.Add(Convert.ToDouble(row[1].ToString()));
            }


            chart1.Series[0]["PieLabelStyle"] = "Outside";//将文字移到外侧 
            chart1.Series[0]["PieLineColor"] = "Black";//绘制黑色的连线。 
            chart1.Series[0].Points.DataBindXY(xData, yData);
            chart1.Series[0].Label = "#PERCENT{P2}";
        }

        public IList<T> DataSetToList<T>(DataSet dataSet, int tableIndex)
        {
            //确认参数有效
            if (dataSet == null || dataSet.Tables.Count <= 0 || tableIndex < 0)
                return null;

            DataTable dt = dataSet.Tables[tableIndex];

            IList<T> list = new List<T>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //创建泛型对象
                T _t = Activator.CreateInstance<T>();
                //获取对象所有属性
                PropertyInfo[] propertyInfo = _t.GetType().GetProperties();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    foreach (PropertyInfo info in propertyInfo)
                    {
                        //属性名称和列名相同时赋值
                        if (dt.Columns[j].ColumnName.ToUpper().Equals(info.Name.ToUpper()))
                        {
                            if (dt.Rows[i][j] != DBNull.Value)
                            {
                                info.SetValue(_t, dt.Rows[i][j], null);
                            }
                            else
                            {
                                info.SetValue(_t, null, null);
                            }
                            break;
                        }
                    }
                }
                list.Add(_t);
            }
            return list;
        }
    }
}
